﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    //员工添加，修改界面
    public partial class Frm_AddStaff : BaseXtraForm
    {
        #region 属性
        /// <summary>
        /// 公司ID
        /// </summary>
        public Guid CompanyID { get; set; }
        public Guid DeptID { get; set; }
        public Guid StaffID { get; set; }

        /// <summary>
        /// 保存执行事件
        /// </summary>
        public event EventHandler SaveEvent;

        /// <summary>
        /// 点击按钮状态
        /// </summary>
        public EStatus FStatus { get; set; }
        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="applicationID"></param>
        /// <param name="status"></param>
        public Frm_AddStaff(Guid companyID,Guid deptID, EStatus status)
        {
            InitializeComponent();

            this.CompanyID = companyID;
            this.DeptID = deptID;
            this.FStatus = status;
        }
        public Frm_AddStaff(Guid staffID, EStatus status)
        {
            InitializeComponent();

            this.StaffID = staffID;
            this.FStatus = status;
        }
        #endregion

        #region 窗体加载方法
        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_AddStaff_Load(object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            //省份数据和毕业院校加载
            Province.Properties.DataSource = aservice.GetAllProvinces();
            Graduation.Properties.DataSource = aservice.GetAllGraduation();

            if (FStatus == EStatus.Add)
            {
                this.Text = "员工 - 添加";
            }
            if (FStatus == EStatus.Modify)
            {
                aservice = new AppService();
                CompanyStaffInfo info = aservice.GetStaffByID(StaffID);
                FName.Text = info.FName;
                Code.Text = info.Code;
                Post.Text = info.Post;
                Wage.Text = info.Wage;
                Province.EditValue = info.ProvinceID;
                Graduation.EditValue = info.Graduation;
                this.Text = "员工 - 修改";
            }
        }
        #endregion

        #region 按钮事件
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            CompanyStaffInfo info = null;
            if (FStatus == EStatus.Add)
            {
                info = aservice.AddStaff(this.CompanyID,this.DeptID, FName.Text.Trim(),Code.Text.Trim(), Post.Text.Trim(), Wage.Text.Trim(), (Guid)Province.EditValue,(string)Graduation.EditValue);
                this.Hide();
                ShowInfo("添加成功!");
            }
            if (FStatus == EStatus.Modify)
            {
                info = aservice.UpdateStaff(this.StaffID, FName.Text,Code.Text, Post.Text, Wage.Text, (Guid)Province.EditValue, (string)Graduation.EditValue);
                this.Hide();
                ShowInfo("编辑成功!");
            }
            if (SaveEvent != null)
            {
                SaveEvent(info, new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region TreeListLookUpEdit中的事件
        /// <summary>
        /// 展开/关闭所有节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Graduation_Popup(object sender, EventArgs e)
        {
            foreach (TreeListNode node in treeList2.Nodes)
            {
                node.Expanded = false;
                SetStyle(node);
            }
        }
        public void SetStyle(TreeListNode node)
        {
            for (var i = 0; i < node.Nodes.Count; i++)
            {
                node.Nodes[i].Expanded = false;
                SetStyle(node.Nodes[i]);
            }
        }

        /// <summary>
        /// 修改节点按钮样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void treeList2_CustomDrawNodeButton(object sender, DevExpress.XtraTreeList.CustomDrawNodeButtonEventArgs e)
        {
            Rectangle rect = Rectangle.Inflate(e.Bounds, 0, -2);

            // painting background
            Brush backBrush = new LinearGradientBrush(rect, Color.Blue, Color.LightSkyBlue,
              LinearGradientMode.ForwardDiagonal);

            //填充指定矩形内部
            e.Graphics.FillRectangle(backBrush, rect);

            //绘画3D边框
            ControlPaint.DrawBorder3D(e.Graphics, rect, Border3DStyle.RaisedOuter);

            //要显示的图画
            string displayCharacter = e.Expanded ? "-" : "+";

            //水平,垂直居中
            StringFormat outCharacterFormat = new StringFormat();
            outCharacterFormat.Alignment = StringAlignment.Center;
            outCharacterFormat.LineAlignment = StringAlignment.Center;

            //绘画图标
            e.Graphics.DrawString(displayCharacter, new Font("Verdana", 8, FontStyle.Bold),
              new SolidBrush(Color.White), rect, outCharacterFormat);

            // 禁止默认的图标显示
            e.Handled = true;
        }
        #endregion

    }
}
